package com.jast.generator.freemarker;

import com.jast.common.utils.file.FileUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jast
 * @description
 * @date 2023-02-14 14:40
 */
public class GenerateHtml {
    /**
     * 基础指标展示模板：单点
     */
    static String basicTemplateName = "basicHtml.ftl";
    /**
     * 折线图展示模板
     */
    static String lineTemplateName = "lineHtml.ftl";
    public static void main(String[] args) throws Exception {
        zookeeperLine();


    }
    public static void zookeeperLine()throws Exception {
        ArrayList<Data> data = new ArrayList<>();
        String original = "a\tzk_watch_count\twatch监听数量\n" +
                "a\tzk_num_alive_connections\t连接数\n" +
                "a\tzk_znode_count\tZookeeper节点数量\n" +
                "a\tzk_avg_latency\t平均延时\n" +
                "a\tzk_max_latency\t最大延时\n" +
                "a\tzk_packets_received\t收包数\n" +
                "a\tzk_packets_sent\t发包数\n" +
                "a\tzk_outstanding_requests\t积压请求数\n" +
                "a\tzk_version\tZookeeper版本";
        for (String line : original.split("\n")) {
            String[] columns = line.split("\t");
            String type = columns[0];
            String name = columns[1];
            String desc = columns[2];
            Data data1 = new Data();
            data1.setDesc(desc);
            data1.setType(type);
            data1.setField(name);
            data.add(data1);
        }
        run(data,"zookeeper-line.txt",lineTemplateName);
    }
    /**
     * 生成Zookeeper基础指标
     * @name yarnBasic
     * @date 2023/2/14 下午3:37
     * @return void

     * @author Jast
     */
    public static void zookeeperBasic()throws Exception {
        ArrayList<Data> data = new ArrayList<>();
        String original = "a\tzk_watch_count\twatch监听数量\n" +
                "a\tzk_num_alive_connections\t连接数\n" +
                "a\tzk_znode_count\tZookeeper节点数量\n" +
                "a\tzk_avg_latency\t平均延时\n" +
                "a\tzk_max_latency\t最大延时\n" +
                "a\tzk_packets_received\t收包数\n" +
                "a\tzk_packets_sent\t发包数\n" +
                "a\tzk_outstanding_requests\t积压请求数\n" +
                "a\tzk_version\tZookeeper版本";
        for (String line : original.split("\n")) {
            String[] columns = line.split("\t");
            String type = columns[0];
            String name = columns[1];
            String desc = columns[2];
            Data data1 = new Data();
            data1.setDesc(desc);
            data1.setType(type);
            data1.setField(name);
            data.add(data1);
        }
        run(data,"zookeeper-basic.txt",basicTemplateName);
    }
    public static void yarnLine()throws Exception {
        ArrayList<Data> data = new ArrayList<>();
        String original = "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemNonHeapCommittedM\tResourceManager JVM当前非堆内存大小已提交大小，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemNonHeapMaxM\tResourceManager JVM非堆最大可用内存，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemNonHeapUsedM\tResourceManager JVM当前已使用的非堆内存大小，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemHeapCommittedM\tResourceManager JVM当前已使用堆内存大小，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemHeapMaxM\tResourceManager JVM堆内存最大可用内存，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tMemHeapUsedM\tResourceManager JVM当前已使用堆内存大小，单位为MB\t基础指标\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tGcTimeMillis\tResourceManager JVM GC时间\tGC\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=JvmMetrics\tGcCount\tResourceManager JVM GC次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=G1 Old Generation\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=G1 Old Generation\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=G1 Young Generation\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=G1 Young Generation\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=ParNew\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=ParNew\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=ConcurrentMarkSweep\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=ConcurrentMarkSweep\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=PS MarkSweep\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=PS MarkSweep\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=PS Scavenge\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "ResourceManager\tjava.lang:type=GarbageCollector,name=PS Scavenge\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tAvailableGB\tNodeManager可用的内存大小，单位为GB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tAllocatedGB\tNodeManager使用的内存大小，单位为GB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tAllocatedVCores\tNodeManager使用的虚拟核数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tAvailableVCores\tNodeManager可用的虚拟核数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersLaunched\tNodeManager Container启动过的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersRunning\tNodeManager Container正在运行的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersFailed\tNodeManager Container失败的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersCompleted\tNodeManager Container运行完成的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersIniting\tNodeManager Container初始化中的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tContainersKilled\tNodeManager Container被中止Kill的个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tBadLocalDirs\tNodeManager磁盘损坏个数\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=NodeManagerMetrics\tGoodLocalDirsDiskUtilizationPerc\tNodeManager磁盘利用率\t基础指标\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=G1 Old Generation\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=G1 Old Generation\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=G1 Young Generation\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=G1 Young Generation\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=ParNew\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=ParNew\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=ConcurrentMarkSweep\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=ConcurrentMarkSweep\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=PS MarkSweep\tCollectionCount\t老年代GC次数/Full GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=PS MarkSweep\tCollectionTime\t老年代GC消耗时间\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=PS Scavenge\tCollectionCount\t新生代GC次数/Young GC 次数\tGC\n" +
                "NodeManager\tjava.lang:type=GarbageCollector,name=PS Scavenge\tCollectionTime\t新生代GC消耗时间\tGC\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemNonHeapCommittedM\tNodeManager JVM当前非堆内存大小已提交大小，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemNonHeapMaxM\tNodeManager JVM非堆最大可用内存，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemNonHeapUsedM\tNodeManager JVM当前已使用的非堆内存大小，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemHeapCommittedM\tNodeManager JVM当前已使用堆内存大小，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemHeapMaxM\tNodeManager JVM堆内存最大可用内存，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tMemHeapUsedM\tNodeManager JVM当前已使用堆内存大小，单位为MB\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tGcTimeMillis\tNodeManager JVM GC时间\t基础指标\n" +
                "NodeManager\tHadoop:service=NodeManager,name=JvmMetrics\tGcCount\tNodeManager JVM GC次数\t基础指标\n" +
                "NodeManager\tjava.lang:type=Runtime\tStartTime\t启动时间戳\t基础指标";
        for (String line : original.split("\n")) {
            String[] columns = line.split("\t");
            String type = columns[0].equals("ResourceManager") ? "r" : "n";
            String name = columns[2];
            String desc = columns[3];
            Data data1 = new Data();
            data1.setDesc(desc);
            data1.setType(type);
            data1.setField(name);
            data.add(data1);
        }
        run(data,"yarn-line.txt",lineTemplateName);
    }
    /**
     * 生成Yarn基础指标
     * @name yarnBasic
     * @date 2023/2/14 下午3:37
     * @return void

     * @author Jast
    */
    public static void yarnBasic()throws Exception {
        ArrayList<Data> data = new ArrayList<>();
        String original = "ResourceManager\tHadoop:service=ResourceManager,name=ClusterMetrics\tNumActiveNMs\t当前存活的 NodeManager 个数\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=ClusterMetrics\tNumDecommissionedNMs\t当前 Decommissioned 的 NodeManager 个数\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=ClusterMetrics\tNumDecommissioningNMs\t集群正在下线的节点数\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=ClusterMetrics\tNumLostNMs\t集群丢失的节点数\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=ClusterMetrics\tNumUnhealthyNMs\t集群不健康的节点数\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=RpcActivityForPort*\tRpcProcessingTimeAvgTime\tHadoop:service=ResourceManager,name=RpcActivityForPort\n" +
                "ResourceManager\tHadoop:service=ResourceManager,name=RpcActivityForPort*\tCallQueueLength\tResourceManager RPC队列积压长度\n" +
                "ResourceManager\tjava.lang:type=Runtime\tStartTime\t启动时间戳";
        for (String line : original.split("\n")) {
            String[] columns = line.split("\t");
            String type = columns[0].equals("ResourceManager") ? "r" : "n";
            String name = columns[2];
            String desc = columns[3];
            Data data1 = new Data();
            data1.setDesc(desc);
            data1.setType(type);
            data1.setField(name);
            data.add(data1);
        }
        run(data,"yarn-basic.txt",basicTemplateName);
    }

    public static void run(ArrayList<Data> dataList,String outputFileName,String templateName)throws Exception {
        // 1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 2.设置模板所在的目录
        configuration.setClassForTemplateLoading(GenerateHtml.class, "/");
        // configuration.setDirectoryForTemplateLoading(new File(""));
        // 3.设置字符集
        configuration.setDefaultEncoding("utf-8");

        configuration.setSetting("classic_compatible", "true");
        // 4.加载模板
        Template template = configuration.getTemplate("freemarker/templates/"+templateName);
        // 5.创建数据模型
        Map map = new HashMap();
        map.put("columns",dataList);


        String dir = System.getProperty("user.dir") + File.separator + "template" + File.separator;
        // 6.创建Writer对象
        Writer out = new FileWriter(new File(dir+outputFileName));
        // 7.输出
        template.process(map, out);
        // 8.关闭Writer对象
        out.close();
    }

    public static class Data{
        private String desc;
        private String field;
        private String type;

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

}
